import { Message } from "element-ui";
import { Toast } from 'vant';


export const iscode = (res) => {
  if (res.code === 0) {
    return true;
  } else {
    let screenWidth =
      document.body.clientWidth || document.documentElement.clientWidth;
    if (screenWidth <= 768) {
      Toast(res.message);
    } else {
      Message({
        showClose: true,
        message: res.message,
        type: 'error'
      });
    }
    return false;
  }
};

export const dayChange = (time) => {
  let date = new Date(time);
  let y = date.getFullYear();
  let m = date.getMonth() + 1;
  m = m < 10 ? "0" + m : m;
  let d = date.getDate();
  d = d < 10 ? "0" + d : d;
  return y + "-" + m + "-" + d;
};


export const getWidth = () => {
  let screenWidth =
    document.body.clientWidth || document.documentElement.clientWidth;
  return screenWidth
}


export const watchlocalstorage = () => {
  const signSetItem = localStorage.setItem;
  localStorage.setItem = function (key, val) {
    const setEvent = new Event('setItemEvent');
    setEvent.key = key;
    setEvent.value = val;
    window.dispatchEvent(setEvent);
    signSetItem.apply(this, arguments);
  };
}


export const getYMDHMS = (timestamp) => {
  let time = new Date(timestamp * 1000)
  let year = time.getFullYear()
  let month = time.getMonth() + 1
  let date = time.getDate()
  let hours = time.getHours()
  let minute = time.getMinutes()
  let second = time.getSeconds()
  if (month < 10) { month = '0' + month }
  if (date < 10) { date = '0' + date }
  if (hours < 10) { hours = '0' + hours }
  if (minute < 10) { minute = '0' + minute }
  if (second < 10) { second = '0' + second }
  return year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
}

export const isPositiveInteger = (s) => {
  var re = /^[0-9]+$/;
  return re.test(s)
}

